(* PermInv.pas *)

program PermInv;

const
   N = 6;

var  
   x : array[1..N] of integer = (6, 2, 1, 5, 4, 3);
   
procedure Print;
var
   i : integer;
begin
   for i := 1 to N do write(x[i],' ');
   writeln();
end; { Print }

var
   m : integer;
   j : integer;
   i : integer;

begin 
   Print();
   m := N;
   j := -1;
   while m > 0 do
   begin
      i := x[m];
      if i < 0 then
      begin
	 x[m] := -i; 
	 m := m-1;
      end
      else
	 while i > 0 do
	 begin
	    x[m] := j;
	    j := -m;
	    m := i;
	    i := x[m];
	 end;
      i := j; 
   end; 
   Print();
end.
